<?php


namespace App\Http\Controllers\Merchant;
use App\Http\Controllers\BaseController;
use App\Models\Balance;
use App\Models\CookbookAdvView;
use App\Models\UnionOrder;
use App\Services\BalanceService;
use Illuminate\Http\Request;

class CookbookAdvViewController extends BaseController
{

    public $model = CookbookAdvView::class;

    public function budget_index(Request $request) {
        $input = $request->input();
        $merchant_id = $this->getUserId();
        $search = ['merchant_id' => $merchant_id];
        $list = CookbookAdvView::apply($search, $input)->where('merchant_id', '=', $merchant_id)
            ->groupBy('month')->selectRaw('month, count(*) as total')->orderBy('month', 'desc')->paginate(per_page());

        foreach ($list as $key => &$item) {
            $item->id = $key + 1;
            $item->finish_total = CookbookAdvView::apply($search, $input)->where('merchant_id', '=', $merchant_id)->where('is_finish', 1)->count();
            $amount = Balance::where('extra', $item->month)->where('merchant_id', $merchant_id)->value('amount');
            $item->amount = $amount ? format_yuan($amount) : null;
            $item->month = substr($item->month, 0, 4) . "-" . substr($item->month, -2, 2);
        }
        return $this->paginate($list);
    }


}

